﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcBoard.Models;
using PagedList;

namespace MvcBoard.Controllers
{
    public class BoardController : Controller
    {
        private EFDbContext db = new EFDbContext();


        
        //
        // GET: /Board/

        public ActionResult Index(int? page,string searchString,string searchChoice,string sortOrder)
        {
            
            var posts = from s in db.Posts select s;


            if(sortOrder == "postNo") // 제목줄 선택 시 정렬방법
                posts = posts.OrderByDescending(s => s.postNo);
            else if (sortOrder == "writer")
                posts = posts.OrderByDescending(s => s.name);
            else if (sortOrder == "subject")
                posts = posts.OrderByDescending(s => s.subject);
            else if (sortOrder == "readcount")
                posts = posts.OrderByDescending(s => s.readCount);
            else if (sortOrder == "writetime")
                posts = posts.OrderByDescending(s => s.writetime);
            else
                posts = posts.OrderByDescending(s => s.postNo);

            ViewBag.sortOrder = String.IsNullOrEmpty(sortOrder) ? "postNo" : sortOrder;

            if (!String.IsNullOrEmpty(searchString)) // 검색
            {
                ViewBag.searchString = searchString;
                ViewBag.searchChoice = searchChoice;

                if(searchChoice == "writer")  // 검색종류
                    posts = posts.Where(s => s.name.ToUpper().Contains(searchString.ToUpper()));
                else if(searchChoice == "subject")
                    posts = posts.Where(s => s.subject.ToUpper().Contains(searchString.ToUpper()));
                else if(searchChoice == "content")
                    posts = posts.Where(s => s.content.ToUpper().Contains(searchString.ToUpper()));                 
            }

            //https://github.com/TroyGoode/PagedList  // PagedList              
            var pageNumber = page ?? 1;
            ViewBag.Page = pageNumber;
            ViewBag.PagePos = posts.ToPagedList(pageNumber, 10);
            return View(ViewBag.PagePos);

        }

        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Index(string searchString)
        //{
        //    if (searchString.Trim().Length == 0)
        //        ModelState.AddModelError("searchString", "검색어를 입력해 주세요");

        //    return View();
        //}

        //
        // GET: /Board/Details/5

        public ActionResult Details(int id = 0)
        {
            Post post = db.Posts.Find(id);
            if (post == null)
            {
                return HttpNotFound();
            }

            post.readCount++;
            db.SaveChanges();
            return View(post);
        }

        //
        // GET: /Board/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Board/Create

        [HttpPost]
        [ValidateAntiForgeryToken] 
        public ActionResult Create(Post post)
        {
            if (ModelState.IsValid)
            {
                post.writetime = DateTime.Now;

                db.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(post);
        }

        //
        // GET: /Board/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Post post = db.Posts.Find(id);
            if (post == null)
            {
                return HttpNotFound();
            }
            return View(post);
        }

        //
        // POST: /Board/Edit/5
        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Post post)
        {
            if (ModelState.IsValid)
            {

                db.Entry(post).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(post);
        }

        //
        // POST: /Board/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Post post = db.Posts.Find(id);
            db.Posts.Remove(post);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}